package cn.bxshey.general.base;

import cn.bxshey.general.constant.JwtConstant;
import cn.bxshey.general.enums.ResultEnum;
import cn.bxshey.general.exception.BusinessException;
import cn.bxshey.general.util.JwtUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;


//获取 request 信息
public class BaseController {

    //获取http信息
    public HttpServletRequest getHttpInfo(){
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    //获取令牌包含的用户信息
    public Map<String,Object> getUserLoginInfo(){
        Map<String, Object> stringObjectMap = JwtUtils.checkToken(getHttpInfo().getHeader(JwtConstant.ACCESS_TOKENS));
System.out.println(stringObjectMap);
        if(Objects.isNull(stringObjectMap)){
             throw new BusinessException(ResultEnum.ERROR_LOGIN);
         }

        return stringObjectMap;
    }

}
